package cn.feichanghao.nutz.quickstart;

import java.lang.reflect.Method;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.RequestPath;
import org.nutz.mvc.View;

import cn.feichanghao.nutz.quickstart.service.MenuService;

public class MenuFilter implements ActionFilter {

	private static final Log log = Logs.getLog(MenuFilter.class);
	private final String MENU_MATCH = "^.+\\.(jsp)$";
	private Pattern matchPtn;

	@Inject
	private MenuService menuService;
	
	@Override
	public View match(ActionContext ac) {
		String regx = MENU_MATCH;
		if (!"null".equalsIgnoreCase(regx)) {
			matchPtn = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);

			// RequestPath path = Mvcs.getRequestPathObject((HttpServletRequest)
			// req);

			if (null == matchPtn || !matchPtn.matcher(ac.getPath()).find()) {
				ac.getRequest().setAttribute("menu", menuService.findAll());
				log.debug("完成菜单设置.");
			}
		}
		return null;

	}

}
